package com.imodia.checkoo.controller;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hsq.commons.util.AuthConfig;
import com.hsq.commons.util.BeanFactory;
import com.hsq.commons.util.ControllerUtil;
import com.hsq.commons.util.tree.TreeNodeBean;
import com.imodia.checkoo.bean.CmsDataDictionaryBean;
import com.imodia.checkoo.bean.CmsUserBean;
import com.imodia.checkoo.service.CmsDepartmetCatalogueService;
import com.imodia.checkoo.service.CmsManagerMenuService;
import com.imodia.checkoo.service.CmsUserService;


public class AuthFilter implements Filter{

	private static final Log log = LogFactory.getLog(AuthFilter.class);
	
	//private CmsManagerMenuService cmsManagerMenuService = (CmsManagerMenuService) BeanFactory.getBean("cmsManagerMenuService");
	

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,FilterChain filterChain) throws IOException, ServletException {
		 long d1 = Calendar.getInstance().getTimeInMillis();
		HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;
        String uri = request.getRequestURI();  
        if((!uri.contains("/login.do"))  
        	&& (!uri.contains("/login.jsp"))
            && (!uri.contains("/images/"))     
            && (!uri.contains("/css/"))){
        	
        	 int userId = ControllerUtil.getAdminId(request);
        	 if(userId>0){
        		 CmsUserService cmsUserService =(CmsUserService) BeanFactory.getBean("cmsUserService");
                 CmsUserBean use = cmsUserService.get(userId);
                 request.getSession().setAttribute(AuthConfig.SESSION_USER_INFO, use);
                 userId = use.getId();
        	 }
        	 if(userId<=0){
        		 request.getRequestDispatcher("/hfmanager/login.jsp").forward(request, response);
         		 return;
        	 }
        }
        loadMenuInfo(request);
        
//        long d2 = Calendar.getInstance().getTimeInMillis();
//        log.info("___________________________________________________________________________");
//        log.info(uri);
//        log.info(d2-d1);
//        log.info("___________________________________________________________________________");
        filterChain.doFilter(servletRequest, servletResponse);
	}
	
	
	public void loadMenuInfo(HttpServletRequest request){

		CmsManagerMenuService cmsManagerMenuService = (CmsManagerMenuService)BeanFactory.getBean("cmsManagerMenuService"); 
	
		CmsDepartmetCatalogueService cmsDepartmetCatalogueService = (CmsDepartmetCatalogueService)BeanFactory.getBean("cmsDepartmetCatalogueService");
		
		Object o = request.getSession().getAttribute(AuthConfig.SESSION_USER_INFO);
		if(o!=null){
				CmsUserBean user = (CmsUserBean)o;
		 		if(request.getSession().getAttribute(AuthConfig.SESSION_MENU_LIST)==null){
		 			List<TreeNodeBean>  menuList = null;
					if(user.getId()>1){
						menuList = cmsManagerMenuService.getManageMenu2ForLogin(user.getDepartmentId());
					}else if(user.getId()==1){
						menuList = cmsManagerMenuService.getManageMenuForLogin();
					}
					request.getSession().setAttribute(AuthConfig.SESSION_MENU_LIST, menuList);
		 		}
				
		 		//���ص�����Ϣ
				if(request.getSession().getAttribute(AuthConfig.SESSION_AREA_LIST)==null){
					List<CmsDataDictionaryBean>  siteList = null;
				if(user.getId()>1){
						siteList = cmsDepartmetCatalogueService.querySiteId(user.getDepartmentId());
					}else if(user.getId()==1){
						siteList = cmsDepartmetCatalogueService.querySite();
					}
					request.getSession().setAttribute(AuthConfig.SESSION_AREA_LIST, siteList);
				}
		}
	}

	
	public void init(FilterConfig arg0) throws ServletException {
	}

	public void destroy() {
	}
}
